﻿using AutoMapper;
using B.S.Procurement.Domain.Extraction;
using B.S.Procurement.Error;
using B.S.Procurement.Read.Api.Application.Command.ExtractionDetailsCommand;
using B.S.Procurement.Read.Api.DTO;
using MediatR;
using SqlSugar;

namespace B.S.Procurement.Read.Api.Application.CommandHander.ExtractionDetailsCommandHander
{
    public class QueryExtractionModelCommandHander : IRequestHandler<QueryExtractionModelCommand, ApiPaging<List<ExtractionDetailsDto>>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public QueryExtractionModelCommandHander(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        public Task<ApiPaging<List<ExtractionDetailsDto>>> Handle(QueryExtractionModelCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<List<ExtractionDetailsDto>> result = new ApiPaging<List<ExtractionDetailsDto>>();

            var item = db.Queryable<ExtractionDetails>()
                       .InnerJoin<ExtractionModel>((a, b) => a.ExtractionId == b.ExtractionId)
                       .Where((a, b) => a.Del == false)
                       .Select((a, b) => new ExtractionDetailsDto
                       {
                           ExtractionDetailsId = a.ExtractionDetailsId,
                           ProductName = a.ProductName,
                           ProductNumber = a.ProductNumber,
                           Specification = a.Specification,
                           Unit = a.Unit,
                           Num = a.Num,
                           Price = a.Price,
                           BillType = a.BillType,
                           Rate = a.Rate,
                           IncludingPrice = a.IncludingPrice,
                           NotTaxedMoney = a.IncludingPrice,
                           Tax = a.Tax,
                           SumMoney = a.SumMoney,
                           OutboundShipments = a.OutboundShipments,
                           lotNumber = a.lotNumber,
                           ExpirationDate = a.ExpirationDate,
                           ManufacturingDate = a.ManufacturingDate,
                           ArrivalDate = a.ArrivalDate,
                           Remark = a.Remark,
                           ExtractionId = b.ExtractionId,
                           Del = a.Del,
                           VendorId = b.VendorId,
                           PurchasingStaff = b.PurchasingStaff,
                           ReturnsPersonnel = b.ReturnsPersonnel,
                           DocumentSubject = b.DocumentSubject,
                           DocumentNumber = b.DocumentNumber,
                           Counterpart = b.Counterpart,
                           Our = b.Our,
                           ReturnType = b.ReturnType,
                           ReturnDate = b.ReturnDate,
                           ProjectId = b.ProjectId,
                           RefundMethods = b.RefundMethods,
                           ReturnMethods = b.ReturnMethods,
                           ReturnAddress = b.ReturnAddress,
                       });

            result.TotalCount = item.Count();
            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
            item = item.OrderByDescending(a => a.ExtractionDetailsId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);

            result.Code = ApiEnum.success;
            result.Msg = "查询成功";
            result.Data = mapper.Map<List<ExtractionDetailsDto>>(item.ToList());

            return Task.FromResult(result);
        }
    }
}
